package modules

import play.api.http._
import play.api.mvc.Results._
import play.api.mvc._
import shared.pojo.Pojo.ErrorResponse

import javax.inject._
import scala.concurrent.Future
import upickle.default._
import implicits.Implicits._
import play.api._
import play.api.routing.Router

/**
 * Created by yz on 10/8/2022
 */
@Singleton
class ErrorHandler @Inject()(
                              env: Environment,
                              config: Configuration,
                              sourceMapper: OptionalSourceMapper,
                              router: Provider[Router],
                            ) extends DefaultHttpErrorHandler(environment = env, configuration = config,
  sourceMapper = sourceMapper, router = router) {

  override def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = {
    exception.printStackTrace()
    val errorResponse = ErrorResponse(exception.getMessage)
    Future.successful(
      InternalServerError(writeJs(errorResponse))
    )
  }

}
